<?php
class User_lottery_model extends CI_Model
{
    private $_table  = 'user_lottery';
	private $_table2 = 'lottery_winner';
    private $_table3 = 'user';

    /**
     * 查找该用户是否有参加
     */
    public function findToday($uid, $lotteryId)
    {
        $this->db->select('id, today_num, weight, entrance');
        $this->db->from($this->_table);
        $this->db->where('uid', $uid);
        $this->db->where('lottery_id', $lotteryId);
        $this->db->where('date', date('Y-m-d'));
        $this->db->limit(1);
        return $this->db->get();
    }

	/**
     * 某用户参加抽奖活动
     */
	public function insertUser($postData)
    {
        $data = array(
		        'uid'          => $postData['uid'],
                'user_name'    => $postData['user_name'],
                'reseller_name'=> empty($postData['reseller_name']) ? '' : $postData['reseller_name'],
				'lottery_id'   => $postData['lottery_id'],
				'weight'       => 0,
                'date'         => date('Y-m-d'),
				'created_at'   => date('Y-m-d H:i:s',time()),
                'today_num'    => $postData['today_num'],
                'today_total'  => $postData['today_num'],
                'entrance'     => $postData['entrance']
        );
        return $this->db->insert($this->_table, $data);
    }

	/**
     * 中奖用户的中奖活动权重增一
	 * @param unknown $uid
	 * @param unknown $lottery_id
     */
    public function increaseWeight($uid,$lottery_id)
    {
		$this->db->set('weight',"weight + 1",false);
        $this->db->set('today_num',"today_num  - 1",false);
        $this->db->where('uid', $uid);
		$this->db->where('lottery_id', $lottery_id);
        $this->db->where('date', date('Y-m-d'));

        return $this->db->update($this->_table);
    }

	/**
     * 剩余抽奖次数减一
	 * @param unknown $uid
     */
	public function decrease_num($uid,$lottery_id)
	{
		$this->db->set('today_num',"today_num - 1",false);
		$this->db->where('uid', $uid);
		$this->db->where('lottery_id', $lottery_id);
        $this->db->where('date', date('Y-m-d'));

		return $this->db->update($this->_table);
	}

    /**
     * 查询某抽奖活动的可能参与人次
     */
    public function total($params, $today = false)
    {
        $this->db->select('COUNT(DISTINCT uid) AS `total`');
        $this->db->from($this->_table);

        if ( !empty($params['lottery_id'])) {
            $this->db->where_in('lottery_id', $params['lottery_id']);
        }

        if ( !empty($params['reseller_name'])) {
            $this->db->where('reseller_name', $params['reseller_name']);
        }

        if ($today) {
            $this->db->where('date', date('Y-m-d'));
        }
        return $this->db->get()->row()->total;
    }

    /**
     * 抽奖参与用户总数
     */
    public function userLotteryTotal($params)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where_in('lottery_id', $params['lottery_id']);
        if ( !empty($params['dateType'])) {
            $startDate = ($params['dateType'] == 'year') ? date('Y-m-d H:i:s', strtotime("-1 year")) : date('Y-m-d H:i:s', strtotime("-{$params['dateType']} day"));
            $this->db->where('created_at > ', $startDate);
            $this->db->where('created_at < ', date('Y-m-d H:i:s'));
        }

        if ( !empty($params['user_name'])) {
            $this->db->like('user_name', $params['user_name'], 'after');
        }
        return $this->db->count_all_results();
    }

    /**
     * 查询抽奖参与用户
     */
    public function userLotteryList($params, $start, $count)
    {
        $this->db->select('id, user_name, date, weight, today_num, today_total, created_at');
        $this->db->from($this->_table);
        $this->db->where_in('lottery_id', $params['lottery_id']);

        if ( !empty($params['dateType'])) {
            $startDate = ($params['dateType'] == 'year') ? date('Y-m-d H:i:s', strtotime("-1 year")) : date('Y-m-d H:i:s', strtotime("-{$params['dateType']} day"));
            $this->db->where('created_at > ', $startDate);
            $this->db->where('created_at < ', date('Y-m-d H:i:s'));
        }

        if ( !empty($params['user_name'])) {
            $this->db->like('user_name', $params['user_name'], 'after');
        }

        $this->db->order_by('id', 'DESC');
        $this->db->limit($count, $start);
        return $this->db->get();
    }
}